package com.ruoyi.tongji.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.jinchuguanli.domain.Xiaoshouchuku;
import com.ruoyi.tongji.domain.QiTaTianJiaReturn;
import com.ruoyi.tongji.domain.RequestQiTaTianJia;
import com.ruoyi.tongji.service.IJieCunService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@Controller
@RequestMapping("/tongji")
public class TongJiController extends BaseController {

    @Autowired
    private IJieCunService jieCunService;

    @RequiresPermissions("tongji:jiecun:view")
    @GetMapping()
    @RequestMapping("/jiecun")
    public String jiecun(HttpServletRequest request) {
        Date date = new Date(System.currentTimeMillis());
        Date oldDate = new Date(date.getTime() - 30L * 24 * 60 * 60 * 1000);

        request.getSession().setAttribute("startTime", new SimpleDateFormat("yyyy-MM-dd").format(oldDate));
        request.getSession().setAttribute("endTime", new SimpleDateFormat("yyyy-MM-dd").format(date));

        return "/tongji/jiecun/jiecun";
    }

    @RequiresPermissions("tongji:churuku:view")
    @GetMapping()
    @RequestMapping("/churuku")
    public String churuku() {
        return "/tongji/churuku/churuku";
    }

    @RequiresPermissions("tongji:qita:view")
    @GetMapping()
    @RequestMapping("/qita")
    public String qita() {
        return "/tongji/qita/qita";
    }

    @GetMapping()
    @RequestMapping("/qita/add")
    public String qitaAdd() {
        return "/tongji/qita/add";
    }

    @RequiresPermissions("tongji:jiecun:list")
    @PostMapping("/jiecun/list")
    @ResponseBody
    public TableDataInfo list(int pageNum, int pageSize, Xiaoshouchuku params) throws ParseException {
        startPage();
        return jieCunService.selectJieCunList(params.getParams().get("beginXiadanriqi").toString(),
                params.getParams().get("endXiadanriqi").toString(),
                pageNum, pageSize);
    }

    @Log(title = "出库", businessType = BusinessType.INSERT)
    @PostMapping("/qita/addSave/{id}")
    @ResponseBody
    public AjaxResult addSave(@PathVariable String id, RequestQiTaTianJia rqt) throws ParseException {
        if (StringUtils.isEmpty(id)) {
            return error("提交过程出现错误，请联系管理员");
        }
        return success(jieCunService.insertRequestQiTaTianJia(id,rqt));
    }

    @PostMapping("/qita/listGet/{id}")
    @ResponseBody
    public TableDataInfo listGet(@PathVariable String id, int pageNum, int pageSize) {
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        TableDataInfo tableDataInfo;
        if ("1".equals(id)) {
            tableDataInfo = jieCunService.selectQiTaList(1, pageNum, pageSize);
        } else {
            tableDataInfo = jieCunService.selectQiTaList(2, pageNum, pageSize);
        }
        return tableDataInfo;
    }

    @GetMapping("/qita/edit/{id}")
    public String edit(@PathVariable String id, ModelMap mmap,HttpServletRequest request) throws ParseException {
        if (StringUtils.isEmpty(id)) {
            return "修改过程出现错误，请联系管理员";
        }
        QiTaTianJiaReturn qiTaTianJiaReturn = jieCunService.selectQiTaTianJia(Long.valueOf(id));
        mmap.put("qiTaTianJiaReturn", qiTaTianJiaReturn);
        request.getSession().setAttribute("TYPE",qiTaTianJiaReturn.getType());
        return "/tongji/qita/edit";
    }

    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/qita/editSave/{id}")
    @ResponseBody
    public AjaxResult editSave(@PathVariable String id, RequestQiTaTianJia qiTaTianJia) throws ParseException {
        if (StringUtils.isEmpty(id)) {
            return error("修改过程出现错误，请联系管理员");
        }
        return success(jieCunService.updateQiTaTianJia(id,qiTaTianJia));
    }

    /**
     * 删除chuku
     */
    @Log(title = "批量删除", businessType = BusinessType.DELETE)
    @PostMapping("/qita/removeSave")
    @ResponseBody
    public AjaxResult remove (String ids) {
        return toAjax(jieCunService.deleteRequestQiTaTianJiaByIds(ids));
    }
}
